package com.jwm.platform;

import cn.hutool.json.JSONUtil;
import com.jwm.model.ClientVo;
import com.jwm.model.CmdInfoVo;
import com.jwm.model.PlteData;
import com.jwm.model.UpdateSortVo;
import com.jwm.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author 崔洁
 */
@Controller
public class PlatController {
    private static final Logger logger = LoggerFactory.getLogger(PlatController.class);
    @Resource
    private PlteData plteData;
    private static final String CMD_ING = "待返回";

    @RequestMapping(method = RequestMethod.GET, path = "/info")
    @ResponseBody
    public AjaxResult info() {
        logger.info("平台端 => 获取信息-开始");
        List<ClientVo> clientVoList = plteData.getClientVoList();
        logger.info("平台端 => 获取信息-结束\n{}", JSONUtil.toJsonStr(clientVoList));
        return AjaxResult.success(clientVoList);
    }

    @RequestMapping(method = RequestMethod.POST, path = "/cmd")
    @ResponseBody
    public AjaxResult cmd(@RequestBody CmdInfoVo cmdInfoVo) {
        logger.info("平台端 = 执行命令-开始\n{}", JSONUtil.toJsonStr(cmdInfoVo));
        List<ClientVo> clientVos = plteData.getClientVoList().stream().filter(clientVo -> clientVo.getName().equals(cmdInfoVo.getClientName())).collect(Collectors.toList());
        if (clientVos.size() > 0) {
            cmdInfoVo.setId(UUID.randomUUID().toString());
            cmdInfoVo.setReturnStr(CMD_ING);
            plteData.getCmdingList().add(cmdInfoVo);
            String cmdInfoString = JSONUtil.toJsonStr(cmdInfoVo);
            ClientVo clientVo = clientVos.get(0);
            if (StringUtils.isNotBlank(clientVo.getCommond())) {
                clientVo.setCommond(clientVo.getCommond() + "^_^" + cmdInfoString);
            } else {
                clientVo.setCommond(cmdInfoString);
            }
        }
        while (plteData.getCmdingList().stream().filter(d -> d.getId().equals(cmdInfoVo.getId())).count() > 0) {
            try {
                Thread.sleep(500);
                for (int i = 0; i < plteData.getCmdingList().size(); i++) {
                    CmdInfoVo cmdingMap = plteData.getCmdingList().get(i);
                    if (cmdingMap.getReturnStr().equals(CMD_ING)) {
                        continue;
                    }
                    if (cmdingMap.getId().equals(cmdInfoVo.getId()) && !cmdingMap.getReturnStr().equals(CMD_ING)) {
                        plteData.getCmdingList().remove(i);
                        logger.info("平台端 => 执行命令-结束\n{}", cmdingMap.getReturnStr());
                        return AjaxResult.success(cmdingMap.getReturnStr());
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                logger.error("平台端 => 执行命令-报错\n{}", e.getMessage());
            }
        }
        logger.info("平台端 => 执行命令-意外结束");
        return AjaxResult.success();
    }

    @RequestMapping(method = RequestMethod.POST, path = "/updateSort")
    @ResponseBody
    public AjaxResult updateSort(@RequestBody UpdateSortVo updateSortVo) {
        logger.info("平台端 => 修改排序\n{}", JSONUtil.toJsonStr(updateSortVo));
        List<ClientVo> clientVos = plteData.getClientVoList().stream().filter(clientVo -> clientVo.getName().equals(updateSortVo.getClientName())).collect(Collectors.toList());
        if (clientVos.size() > 0) {
            ClientVo clientVo = clientVos.get(0);
            clientVo.setSort(updateSortVo.getSort());
        }
        logger.info("平台端 = 修改排序结束");
        return AjaxResult.success();
    }

    @RequestMapping(method = RequestMethod.GET, path = "/error")
    @ResponseBody
    public AjaxResult error() {
        logger.info("平台端 => 鉴权失败！");
        return AjaxResult.error();
    }
}

